//
//  FutureCollectionView.m
//  天气预报
//
//  Created by mac on 16/1/21.
//  Copyright © 2016年 YF_S. All rights reserved.
//

#import "FutureCollectionView.h"
#import "FutureCollectionViewCell.h"
#import "LifeView.h"
#import "FutureWeatherModel.h"
#import "PM25View.h"
#import "PM25Model.h"

@implementation FutureCollectionView{

    LifeView *_liftView;//生活
    
    NSMutableArray *futureDataArr;
    
    PM25View *_pm25View;
}

-(instancetype)initWithFrame:(CGRect)frame{

    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    
    //设置滑动方向
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    //设置单元格的大小
    layout.itemSize = CGSizeMake(100, 320);

    layout.minimumInteritemSpacing = 0;
    
    layout.minimumLineSpacing = 0;
    
    if ([super initWithFrame:frame collectionViewLayout:layout]) {
        
        self.dataSource = self;
        self.delegate = self;
        
        if (!_liftView) {
            
            _liftView = [[[NSBundle mainBundle] loadNibNamed:@"LifeView" owner:nil options:nil] objectAtIndex:0];
            
            _liftView.top = 0;
            
            _liftView.backgroundColor = [UIColor clearColor];
            
        }//头视图
        
        if (!_pm25View) {
            
            _pm25View = [[[NSBundle mainBundle] loadNibNamed:@"PM25View" owner:nil options:nil] objectAtIndex:0];
            
            _pm25View.top = 0;
            
            _pm25View.backgroundColor = [UIColor clearColor];
            
        }//尾视图

        
        self.backgroundColor = [UIColor clearColor];
        
        //注册单元格
        [self registerClass:[FutureCollectionViewCell class] forCellWithReuseIdentifier:@"cell1"];
        
        [self registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
        
        [self registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"];
    }
    
    return self;
}

-(void)setResult:(NSDictionary *)result{

    _result = result;
    
    futureDataArr = [NSMutableArray array];
    
    //
    NSDictionary *result1 = [_result objectForKey:@"result"];
    
    NSDictionary *data = [result1 objectForKey:@"data"];
    
    //life
    NSDictionary *life = [data objectForKey:@"life"];
    
    NSDictionary *info = [life objectForKey:@"info"];

    _liftView.info = info;
    
    //未来几天
    NSArray *weather = [data objectForKey:@"weather"];
    
    for (NSDictionary *dic in weather) {
        
        FutureWeatherModel * model = [[FutureWeatherModel alloc]initWithDic:dic];
        
        [futureDataArr addObject:model];
    }


    //pm25
    NSDictionary *pm25 = [data objectForKey:@"pm25"];
    
    PM25Model *model = [[PM25Model alloc]initWithDic:pm25];
    
    _pm25View.pm25Model = model;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    return futureDataArr.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    FutureCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell1" forIndexPath:indexPath];
    
    cell.model = futureDataArr[indexPath.row];
    
    return cell;
}

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{

    if (kind == UICollectionElementKindSectionHeader) {
        
        UICollectionReusableView *headerCell = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
        
        [headerCell addSubview:_liftView];
        
        return headerCell;
    }else{
    
        UICollectionReusableView *footer = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"footer" forIndexPath:indexPath];
        
        [footer addSubview:_pm25View];
        
        return footer;
    }
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{

    return CGSizeMake(kScreenW, 280);
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{

    return CGSizeMake(kScreenW, 100);
}

//
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{

    return UIEdgeInsetsMake(10, 10, 10, 10);
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
